package com.ht.aems.fesservice;

import com.ht.aems.fesmodel.FesProperties;
import com.ht.aems.fesmodel.FesServer;
import com.ht.aems.fesservice.job.LogBackupJob;
import com.ht.aems.fesservice.job.SampleJob;
import com.ht.log.LoggerUtil;
import com.ht.log.RtAlarmUtil;
import com.ht.memcached.MemCachedConfigurator;
import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.text.ParseException;
import java.util.Date;

/**
 * main class of fes system
 *
 * @author Dong shufeng Date: 2006-12-4
 */
public class FesServerMain {

    private static Logger log = Logger.getLogger(FesServerMain.class);

    public FesServerMain() {
        super();
    }

    public static void main(String[] args) throws Exception {
        FesProperties.readConf("config.fesserver");
        int port = Integer.parseInt(FesProperties.getProperty(FesProperties.FES_SERVER_PORT));
        if (args != null && args.length > 0 && args[0] != null) {
            if (args[0].equalsIgnoreCase("start")) {
                FesServer sender;
                try {
                    sender = (FesServer) Naming.lookup("//127.0.0.1:" + port + "/" + FesServer.class.getName());
                } catch (Exception e) {
                    sender = null;
                }
                if (sender != null) {
                    log.info("fes server is already start at: //127.0.0.1:" + port + "/" + FesServer.class.getName());
                } else {
                    LocateRegistry.createRegistry(port);
                    Naming.rebind("//127.0.0.1:" + port + "/" + FesServer.class.getName(), RemoteFesServer.getInstance());
                    //MemCachedConfigurator.config(FesServerMain.class.getResourceAsStream("/config/memcached.properties"));
                    RemoteFesServer.getInstance().startJob();
                    LoggerUtil.initialBufferSize(50);
                    RtAlarmUtil.startAlarmThread();
                    log.info("fes server is ready at: //127.0.0.1:" + port + "/" + FesServer.class.getName());
                }
            } else if (args[0].equalsIgnoreCase("stop")) {
                FesServer sender;
                try {
                    sender = (FesServer) Naming.lookup("//127.0.0.1:" + port + "/" + FesServer.class.getName());
                } catch (Exception e) {
                    // log.info(e);
                    sender = null;
                }
                if (sender != null) {
                    sender.releaseResource();
                }
                log.info("fes server is stopped");
            } else if (args[0].equalsIgnoreCase("status")) {
                try {
                    FesServer sender = (FesServer) Naming.lookup("//127.0.0.1:" + port + "/" + FesServer.class.getName());
                    if (sender != null)
                        System.out.println("fes server is active!");
                    else
                        System.out.println("fes server is stopped!");
                } catch (Exception e) {
                    log.warn(e);
                    System.out.println("fes server is stopped!");
                }
            } else if (args[0].equalsIgnoreCase("stand-alone")) {
                FesServer sender;
                try {
                    sender = (FesServer) Naming.lookup("//127.0.0.1:" + port + "/" + FesServer.class.getName());
                } catch (Exception e) {
                    sender = null;
                }
                if (sender != null) {
                    log.info("fes server is already start at: //127.0.0.1:" + port + "/" + FesServer.class.getName());
                } else {
                    LocateRegistry.createRegistry(port);
                    LoggerUtil.initialBufferSize(50);
                    MemCachedConfigurator.config(FesServerMain.class.getResourceAsStream("/config/memcached.properties"));
                    RemoteFesServer.getInstance().startJob();
                    RemoteFesServer.getInstance().setPrimeMachine(true);
                    RemoteFesServer.getInstance().setStandAlone(true);
                    RemoteFesServer.getInstance().setAutoConnect(true);
                    startLogBackupJob();
                    RtAlarmUtil.startAlarmThread();
                    Naming.rebind("//127.0.0.1:" + port + "/" + FesServer.class.getName(), RemoteFesServer.getInstance());
                    log.info("fes server is ready at: //127.0.0.1:" + port + "/" + FesServer.class.getName() + " as stand-alone mode");
                }
            } else
                log.info("usage: run [start | stop | status]");
        } else {
            main(new String[]{"start"});
        }
    }

    private static void startLogBackupJob() throws SchedulerException, ParseException {
        JobDetail job1 = new JobDetail("LogBackup", "fes", LogBackupJob.class);
        RemoteFesServer.getInstance().getJobDispatcher().getScheduler().addJob(job1, true);

        // 2. trigger
        CronTrigger trigger1 = new CronTrigger("LogBackupTrigger", "fes", "LogBackup", "fes", "0 33 1 * * ?");
        Date ft = RemoteFesServer.getInstance().getJobDispatcher().getScheduler().scheduleJob(trigger1);
        log.info(job1.getFullName() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger1.getCronExpression());
    }
}
